package com.lu.pojo.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

/**
 * @author luzhenfang
 * @date 2020 07 07
 * @description swing Jframe
 */

public class SampleFrameTest {
    public static void main(String[] args) {
        // 获取系统字体
        GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] availableFontFamilyNames = localGraphicsEnvironment.getAvailableFontFamilyNames();
        for (String availableFontFamilyName : availableFontFamilyNames) {
            System.out.println(availableFontFamilyName);
        }
        // 事件分派线程
        EventQueue.invokeLater(() -> {
            SampleFrame sampleFrame = new SampleFrame();
            // 关闭动作响应
            sampleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // 居中窗体
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = toolkit.getScreenSize();
            sampleFrame.setLocation(screenSize.width / 2 - SampleFrame.DEFAULE_WIDTH / 2, screenSize.height / 2 - SampleFrame.DEFAULE_HEIGHT / 2);
            // 标题
            sampleFrame.setTitle("Hello Swing");

            // 内部组件
            SampleComponent sampleComponent = new SampleComponent(70, 180);
            Sample2DComponent sample2DComponent = new Sample2DComponent();
//            sampleFrame.add(sampleComponent);
            sampleFrame.add(sample2DComponent);
            // 设置背景颜色
            sampleFrame.getContentPane().setBackground(Color.black);
            // 窗体可见
            sampleFrame.setVisible(true);
            // 调整窗体大小
//            sampleFrame.pack();
        });
    }
}


// 2D 控件
class Sample2DComponent extends JComponent {
    @Override
    protected void paintComponent(Graphics g) {
        // 转换成 2d 对象
        Graphics2D g2 = (Graphics2D) g;
        // 开启抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 设置画笔颜色
        g2.setPaint(Color.red);
        // 绘制矩形
        Rectangle2D rectangle2D = new Rectangle2D.Double(30, 30, 200, 60);
        // 填充
        g2.fill(rectangle2D);
        // 绘制文本
        g2.setPaint(Color.white);
        // 设置字体
        Font font = new Font("华文彩云", Font.BOLD, 20);
        g2.setFont(font);
        g2.drawString("FBI WARING", 60, 70);


        // 字体渲染
        FontRenderContext fontRenderContext = g2.getFontRenderContext();
        // 获取字体矩形
        Rectangle2D fbi_waring = font.getStringBounds("FBI WARING", fontRenderContext);
        // 宽度 高度 上坡度
        double width = fbi_waring.getWidth();
        double height = fbi_waring.getHeight();
        double y = fbi_waring.getY();
        // 下坡度  行间距
        LineMetrics lineMetrics = font.getLineMetrics("Hello,World",fontRenderContext);
        float descent = lineMetrics.getDescent();
        float leading = lineMetrics.getLeading();
        // 画
        g2.draw(rectangle2D);
    }
}


// 普通控件
class SampleComponent extends JComponent {
    private final int left;
    private final int top;
    // 字体
    private Font font = new Font("微软雅黑", Font.BOLD, 25);

    public SampleComponent(int left, int top) {
        this.left = left;
        this.top = top;
    }

    // 组件绘制
    @Override
    protected void paintComponent(Graphics g) {
//        g.setFont(font);
        g.drawString("Hello,World", left, top);
    }


    // 组件大小
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(left, top);

    }
}


class SampleFrame extends JFrame {
    // 定义窗体 宽高
    public static final int DEFAULE_WIDTH = 270;
    public static final int DEFAULE_HEIGHT = 180;

    public SampleFrame() throws HeadlessException {
        setSize(DEFAULE_WIDTH, DEFAULE_HEIGHT);
    }
}


